package chapter8;

import chapter5.entity.Admin;
import chapter5.service.AdminService;
import chapter5.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/upd")
public class UpdServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求编码格式
        request.setCharacterEncoding("utf-8");
        //设置响应的格式
        response.setContentType("text/html;charset=utf-8");
        //设置响应的编码格式
        response.setCharacterEncoding("utf-8");
        int id=Integer.parseInt(request.getParameter("id"));
        String name=request.getParameter("name");
        String pwd=request.getParameter("pwd");
        PrintWriter out=response.getWriter();
        Admin admin=new Admin(id,name,pwd);
        AdminService impl=new AdminServiceImpl();
        int count=impl.update(admin);
        if(count>0){
            out.println("<script type=\"text/javascript\">\n" +
                    "    alert(\"修改成功\");\n" +
                    "    location.href=\"http://localhost:8080/untitled1_war_exploded/eee\";\n" +
                    "</script>");
        }else{
            out.println("<script type=\"text/javascript\">\n" +
                    "    alert(\"修改失败\");\n" +
                    "    location.href=\"http://localhost:8080/untitled1_war_exploded/eee\";\n" +
                    "</script>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
